package com.thiny.android.game.thunderbolt.thread;

import java.util.ArrayList;
import java.util.List;

import com.thiny.android.game.thunderbolt.callback.BeFiredCallback;
import com.thiny.android.game.thunderbolt.callback.KillBossCallBack;
import com.thiny.android.game.thunderbolt.role.Bullet;
import com.thiny.android.game.thunderbolt.role.Enemy;
import com.thiny.android.game.thunderbolt.role.Explode;
import com.thiny.android.game.thunderbolt.role.Player;
import com.thiny.android.game.thunderbolt.role.Property;
import com.thiny.android.game.thunderbolt.util.Constants;
import com.thiny.android.game.thunderbolt.util.LogUtil;
import com.thiny.android.game.thunderbolt.util.SoundManager;
import com.thiny.android.game.thunderbolt.view.GameView;

/**
 * 
 * 控制线程类
 * 
 * 除主战机外，各种角色的移动，敌机，子弹，等等；同时，控制我方战机，敌机子弹的发射
 * 
 * @author THINY
 * 
 */

public class ControlThread extends Thread{
	// GameView对象
	private GameView mGameView;
	// 声音管理对象
	private SoundManager mSoundManager;
	// 循环标识
	private boolean mFlag;
	// boss是否出现
	private boolean isBossOn;
	// 是否暂定
	public boolean isOnPause;
	// 每多少次进行一次移动
	private int enemyMovementTimer = 0;
	private int enemyMovementNum = 3;
	// 敌人子弹移动控制
	private int enemyBulletMovementTimer = 0;
	private int enemyBulletMovementNum = 3;
	// 玩家子弹移动控制
	private int playerBulletMovementTimer = 0;
	private int playerBulletMovementNum = 3;
	// 宝物移动控制
	private int propertyMovementTimer = 0;
	private int propertyMovementNum = 3;

	// 增加敌方战机
	private int addEnemyTimer = 0;
	private static final int addEnemyNum = 100;

	// BOSS出现计时
	private int addBossTimer = 1;

	// 我方射击
	private int playerFireTimer = 0;
	private int playerFireNum = 30;

	// 敌方射击
	private int enemyFireTimer = 0;
	private static final int enemyFireNum = 100;

	// 我方飞机发出的子弹list
	ArrayList<Bullet> deletePlayerBullets = new ArrayList<Bullet>();
	// 敌方飞机发出的子弹list
	ArrayList<Bullet> deleteEnemyBullets = new ArrayList<Bullet>();
	// 敌方的飞机list
	ArrayList<Enemy> deleteEnemies = new ArrayList<Enemy>();

	private KillBossCallBack mKillBossCallBack;

	private BeFiredCallback mBeFiredCallback;

	public ControlThread(GameView gameView) {
		mGameView = gameView;
		mFlag = true;
		mSoundManager = SoundManager.getInstance(gameView.getContext());
	}

	public void setFlag(boolean mFlag) {
		this.mFlag = mFlag;
	}

	public void setKillBossCallBack(KillBossCallBack killBossCallBack) {
		mKillBossCallBack = killBossCallBack;
	}

	public void setBeFiredCallback(BeFiredCallback beFiredCallback) {
		mBeFiredCallback = beFiredCallback;
	}

	@Override
	public void run() {
		while (mFlag) {
			if (!isOnPause) {
				// 添加敌机
				if (addEnemyTimer == 0) {
					if (mGameView.getEnemies().size() < 6) {
						mGameView.addEnemy();
					}
				}
				// 添加BOSS
				if (!isBossOn && addBossTimer == 0) {
					mGameView.addBoss();
					isBossOn = true;
				}
				// 玩家射击
				if (playerFireTimer == 0) {
					playerFire();
				}
				// 敌人射击
				if (enemyFireTimer == 0) {
					enemyFire();
				}
				// 玩家子弹移动
				if (playerBulletMovementTimer == 0) {
					playerBulletsMovement();
				}
				// 敌机移动
				if (enemyMovementTimer == 0) {
					enemiesMovement();
				}
				// 敌机子弹移动
				if (enemyBulletMovementTimer == 0) {
					enemyBulletsMovement();
				}
				// 道具移动
				if (propertyMovementTimer == 0) {
					propertyMovement();
				}
				// 背景移动
				backgroundMovement();
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				// 处理各种计数器问题
				playerBulletMovementTimer = (playerBulletMovementTimer + 1)
						% playerBulletMovementNum;
				enemyBulletMovementTimer = (enemyBulletMovementTimer + 1)
						% enemyBulletMovementNum;
				enemyMovementTimer = (enemyMovementTimer + 1)
						% enemyMovementNum;
				propertyMovementTimer = (propertyMovementTimer + 1)
						% propertyMovementNum;
				playerFireTimer = (playerFireTimer + 1) % playerFireNum;
				enemyFireTimer = (enemyFireTimer + 1) % enemyFireNum;
				addEnemyTimer = (addEnemyTimer + 1) % addEnemyNum;

				if (!isBossOn) {
					addBossTimer = (addBossTimer + 1) % Constants.ADD_BOSS_NUM;
				}
			}
		}

	}

	// 我方射击
	private void playerFire() {
		if (mGameView.isFire()) {
			mGameView.playerFire();
			mSoundManager.playSound(Constants.SOUND_KEY_SHOT);
		}
	}

	// 我方战机子弹的移动
	private void playerBulletsMovement() {
		List<Bullet> bullets = mGameView.getPlayerBullets();
		List<Enemy> enemies = mGameView.getEnemies();

		for (int i = 0; i < bullets.size(); i++) {
			Bullet bullet = bullets.get(i);
			if (bullet == null) {
				continue;
			}
			bullet.move();
			// 检测是否击中敌方战机
			for (int j = 0; j < enemies.size(); j++) {
				Enemy enemy = enemies.get(j);
				if (bullet.isIntersect(enemy.getRoleRect())) {
					int harm = Constants.HARM_BULLET_1;
					if (bullet.getType() == Constants.PLAYER_BULLET_2) {
						harm = Constants.HARM_BULLET_2;
					}
					enemy.life -= harm;
					
					deletePlayerBullets.add(bullet);
//					bullets.remove(bullet);
					if (enemy.life <= 0) {
						// boss被杀时候回调
						if (enemy.isBoss()&&enemy.isAlive() && mKillBossCallBack != null) {
							mKillBossCallBack.onBossKilled();
							isBossOn = false;
							// boss爆炸声音
							mSoundManager
									.playSound(Constants.SOUND_KEY_BOSS_EXPLODE);
						}
						killEnemy(enemies, enemy);
					} else {
						if (enemy.isBoss()) {
							mGameView
									.addExplode(new Explode(mGameView, bullet));
						}
					}
					// 子弹爆炸声
					mSoundManager.playSound(Constants.SOUND_KEY_BULLET_EXPLODE);
				}
				if (bullet.isOutOfScreen()) {
//					bullets.remove(bullet);
					deletePlayerBullets.add(bullet);
				}
			}
		}
		enemies.removeAll(deleteEnemies);
		deleteEnemies.clear();
		bullets.removeAll(deletePlayerBullets);
		deletePlayerBullets.clear();
	}

	// 敌方战机的移动
	private void enemiesMovement() {
		List<Enemy> enemies = mGameView.getEnemies();
		if (enemies == null) {
			return;
		}
		for (int i = 0; i < enemies.size(); i++) {
			Enemy enemy = enemies.get(i);
			enemy.move();
			if (enemy.isIntersect(mGameView.getPlayer().getRoleRect())
					&& mGameView.getPlayer().isAlive()) {
				LogUtil.i("敌方战机与我方碰撞！");
				if (!enemy.isBoss()) {
					killEnemy(enemies, enemy);
				}
				mGameView.getPlayer().delLife();

				if (mBeFiredCallback != null) {
					mBeFiredCallback.onPlayerBeFired();
				}
			}
			if (enemy.isOutOfScreen()) {
//				enemies.remove(enemy);
				deleteEnemies.add(enemy);

			}
		}
		enemies.removeAll(deleteEnemies);
		deleteEnemies.clear();
	}

	// 敌方射击
	private void enemyFire() {
		List<Enemy> enemies = mGameView.getEnemies();
		if (enemies == null) {
			return;
		}
		for (int i = 0; i < enemies.size(); i++) {
			mGameView.enemyFire(enemies.get(i));
		}
	}

	// 敌方子弹的移动
	private void enemyBulletsMovement() {
		List<Bullet> bullets = mGameView.getEnemyBullets();
		if (bullets == null) {
			return;
		}

		for (int i = 0; i < bullets.size(); i++) {
			Bullet bullet = bullets.get(i);
			if (bullet == null) {
				continue;
			}
			bullet.move();
			if (bullet.isIntersect(mGameView.getPlayer().getRoleRect())
					&& mGameView.getPlayer().isAlive()) {
				if (mBeFiredCallback != null) {
					mBeFiredCallback.onPlayerBeFired();
				}
//				bullets.remove(bullet);
				deleteEnemyBullets.add(bullet);
			}
			if (bullet.isOutOfScreen()) {
//				bullets.remove(bullet);
				deleteEnemyBullets.add(bullet);

			}
		}
		bullets.removeAll(deleteEnemyBullets);
		deleteEnemyBullets.clear();
	}

	// 宝物道具的移动
	private void propertyMovement() {
		List<Property> properties = mGameView.getProperties();
		if (properties == null) {
			return;
		}
		for (int i = 0; i < properties.size(); i++) {
			Property property = properties.get(i);
			if (property == null) {
				continue;
			}
			property.move();
			if (property.isIntersect(mGameView.getPlayer().getRoleRect())
					&& mGameView.getPlayer().isAlive()) {

				dealProperty(property);
				mSoundManager.playSound(Constants.SOUND_KEY_GET_PROP);
				properties.remove(property);
			}
			if (property.isOutOfScreen()) {
				properties.remove(property);
			}
		}

	}

	/**
	 * 背景滚动
	 */
	private void backgroundMovement() {
		mGameView.backgroundY = mGameView.backgroundY >= mGameView.backgroundBitmap
				.getHeight() ? mGameView.backgroundY
				- mGameView.backgroundBitmap.getHeight() + 1
				: mGameView.backgroundY + 1;
	}

	// 玩家获得道具后，此处处理
	private void dealProperty(Property property) {
		Player player = mGameView.getPlayer();

		switch (property.getType()) {
		case Constants.ADD_LIFE:
			player.addLife();
			break;
		case Constants.ADD_BULLET:
			if (player.getType() == Constants.PLAYER_TYPE_1) {
				player.setType(Constants.PLAYER_TYPE_2);
			} else if (player.getType() == Constants.PLAYER_TYPE_2) {
				player.setType(Constants.PLAYER_TYPE_3);
			}
			break;
		case Constants.CHANGE_BULLET:
			player.setBulletType(Constants.PLAYER_BULLET_2);
			break;
		case Constants.SPEED_UP:
			player.bulletSpeedUp();
			if (playerFireNum > 25) {
				playerFireNum--;
			}
			break;
		case Constants.CLEAR_ALL_ENEMY:
			if (!isBossOn) {
				mGameView.clearAll();
			}
			break;
		}
		// 吃到道具，统一加500分
		mGameView.score += 500;
	}

	// 处理敌军被杀
	private void killEnemy(List<Enemy> enemies, Enemy enemy) {
		enemy.setAlive(false);
		mGameView.addExplode(new Explode(mGameView, enemy));
//		enemies.remove(enemy);
		deleteEnemies.add(enemy);
		switch (enemy.getType()) {
		case Constants.ENEMY_TYPE_1:
			mGameView.score += 100;
			break;
		case Constants.ENEMY_TYPE_2:
			mGameView.score += 200;
			break;
		case Constants.ENEMY_TYPE_3:
			mGameView.score += 300;
			break;
		case Constants.ENEMY_TYPE_4:
			mGameView.score += 500;
			break;
		case Constants.ENEMY_TYPE_BOSS1:
			mGameView.score += 1000;
			break;
		case Constants.ENEMY_TYPE_BOSS2:
			mGameView.score += 2000;
			break;
		case Constants.ENEMY_TYPE_BOSS3:
			mGameView.score += 5000;
			break;
		}

		// 1/2的机会生成物品道具
		if (mGameView.mRandom.nextBoolean()) {
			mGameView.addProperty(enemy);
		}
	}

}
